package com.example.activities;

import com.example.demo.R;
import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ProgressBar;
import android.widget.TextView;

public class AsyncTaskPostDataPage extends ActionBarActivity {
	ProgressBar pro;
	TextView tv;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_async_task_post_data_page);
		pro = (ProgressBar) findViewById(R.id.proPostData);
		pro.setVisibility(View.GONE);
		tv = (TextView) findViewById(R.id.txtState);
		final SharedPreferences prefs = getSharedPreferences("alarmCheck",
				MODE_PRIVATE);
		boolean alarmUp = prefs.getBoolean("alarm", false);
		if (alarmUp) {
			((TextView) (findViewById(R.id.txtState)))
					.setText("Posting data is started! U can only stop it!");
			((TextView) (findViewById(R.id.txtState))).setTextColor(Color.RED);
			findViewById(R.id.btnStartPostData).setAlpha((float) 0.4);
			findViewById(R.id.btnStartPostData).setClickable(false);
		} else {

		}
		findViewById(R.id.btnStartPostData).setOnClickListener(
				new OnClickListener() {
					@SuppressLint("CommitPrefEdits")
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						SharedPreferences.Editor edit = prefs.edit();
						edit.putBoolean("alarm", true);
						edit.commit();
						startAlarm();
						new AsyncExitApp().execute();
					}
				});
		findViewById(R.id.btnStopPostData).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
						Intent intent = new Intent(AsyncTaskPostDataPage.this,
								com.example.common.UpdateDataBroadcast.class);
						PendingIntent penIntent = PendingIntent.getBroadcast(
								AsyncTaskPostDataPage.this, 0, intent,
								PendingIntent.FLAG_UPDATE_CURRENT);
						alarmManager.cancel(penIntent);
						SharedPreferences.Editor edit = prefs.edit();
						edit.putBoolean("alarm", false);
						edit.commit();
						((TextView) (findViewById(R.id.txtState)))
								.setText("Stopped posting data!");
						((TextView) (findViewById(R.id.txtState)))
								.setTextColor(Color.BLUE);
						findViewById(R.id.btnStartPostData).setAlpha(
								(float) 1.0);
						findViewById(R.id.btnStartPostData).setClickable(true);
					}
				});
	}

	private class AsyncExitApp extends AsyncTask<Void, Void, Void> {
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			((TextView) (findViewById(R.id.txtState)))
					.setText("Start posting data and back to homepage after "
							+ " 3 second!");
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			for (int i = 2; i >= 0; i--) {
				final int count = i;
				doFakeWork();
				runOnUiThread(new Runnable() {
					public void run() {
						((TextView) (findViewById(R.id.txtState)))
								.setText("Start posting data and back to homepage after "
										+ count + " second!");
					}
				});
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			System.exit(1);
		}

	}

	private void doFakeWork() {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private void startAlarm() {
		Intent intent = new Intent(AsyncTaskPostDataPage.this,
				com.example.common.UpdateDataBroadcast.class);
		PendingIntent penIntent = PendingIntent.getBroadcast(
				AsyncTaskPostDataPage.this, 0, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);

		AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
		alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
				System.currentTimeMillis() + 3 * 1000, 10 * 1000 * 1000,
				penIntent);
	}
}
